<?php
require('../../../../lib/base.php');
$base=new base('web');
$session=$base->library('session',array('build'=>false));
if(!$session->ISSESS()){
	$helper=$base->library('helper');
	$helper->SMessage(5,domain.'view/Login.php?back='.domain.'home/account/bank.php',"no","登陆失效,5秒后转到登陆页");
	exit();
}
$data['menu']=$base->cache(array('tag'=>'web.header','time'=>86400));
$data['menu']=unserialize($data['menu']);
$data['HomeMenu']=$base->cache(array('tag'=>'web.home.menu','time'=>86400));
$data['HomeMenu']=unserialize($data['HomeMenu']);
$data['foot']=$base->cache(array('tag'=>'web.footer','time'=>86400));
$sort=$base->model('sort');
$data['bank']=$sort->GSort(array('select'=>array('id','name','parent'),'where'=>array('label='=>'bank','locked='=>0),'order'=>array('sequence'=>'DESC')));
$data['china']=$sort->SortTree(array('select'=>array('id','name','parent'),'where'=>array('label ='=>'china','locked='=>0),'order'=>array('sequence'=>'DESC')));
$bank=$base->model('bank');
$bankbook=$bank->GBank(array('where'=>array('member ='=>$session->GVAR('member'))));
if(!empty($bankbook)){
	$bankbook=reset($bankbook);
	$data['type']=$bankbook['type'];
	$data['company']=$bankbook['company'];
	$data['subbranch']=$bankbook['subbranch'];
	$data['province']=$bankbook['province'];
	$data['city']=$bankbook['city'];
	$data['account']=$bankbook['account'];
	$data['name']=$bankbook['name'];
	$data['remark']=$bankbook['remark'];
	$data['locked']=$bankbook['locked'];
}else{
	$data['name']=$session->GVAR('name');
}
if($base->post('submit',true)){
	$data['message']=array('result'=>false,'msg'=>array());
	$type=intval($base->post('type',true));
	$company=intval($base->post('company',true));
	$subbranch=trim($base->post('subbranch',true));
	$province=intval($base->post('province',true));
	$city=intval($base->post('city',true));
	$account=trim($base->post('account',true));
	$name=trim($base->post('name',true));
	if($company==0){
		$data['message']['msg'][]='开户银行不能为空.';
	}else if(!preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$subbranch)){
		$data['message']['msg'][]='开户支行只能为汉字，不包含数字及其它字符.';
	}else if($province==0){
		$data['message']['msg'][]='开户银行所在省不能为空.';
	}else if($city==0){
		$data['message']['msg'][]='开户银行所在市不能为空.';
	}else if(!preg_match("/^[0-9]{1,32}$/",$account)){
		$data['message']['msg'][]='请输入正确的银行账号（数字,不包含空格及其它特殊字符）.';
	}else if($name==''){
		$data['message']['msg'][]='开户人姓名不能为空.';
	}else{
		if(!empty($bankbook)){
			$affected=$bank->UBank(array('set'=>array('type ='=>$type,'company ='=>$company,'subbranch ='=>$subbranch,'province ='=>$province,'city ='=>$city,'account ='=>$account,'name ='=>$name,'locked ='=>0),'where'=>array('member ='=>$session->GVAR('member'))));
		}else{
			$affected=$bank->IBank(array('member'=>$session->GVAR('member'),'type'=>$type,'company'=>$company,'subbranch'=>$subbranch,'province'=>$province,'city'=>$city,'account'=>$account,'name'=>$name));
		}
		if($affected!=1){
			$data['message']['msg'][]='请误重复提交,如需修改请修改后再提交.';
		}else{
			$session->SVAR('bank',$name);
			$data['type']=$type;
			$data['company']=$company;
			$data['subbranch']=$subbranch;
			$data['province']=$province;
			$data['city']=$city;
			$data['account']=$account;
			$data['name']=$name;
			$data['locked']=1;
			$data['message']['result']=true;
			$data['message']['msg'][]='收款帐户修改成功.';
		}
	}
}
$base->set($data);
$base->view('home/payment/bank.php');
?>